package com.suray.wcs.service.hk.agv.core;

import com.suray.commin.core.utils.BeanUtils;
import com.suray.wcs.service.hk.agv.extern.function.HkAgvExternFunc;
import com.suray.wcs.service.hk.agv.type.HkAgvTaskStatus;
import com.suray.wcs.service.util.WcsUtil;

import java.util.Date;
import java.util.Objects;

/**
 * 海康AGV任务信息
 */
public class HkAgvTask {

    private static int taskIdSeed = 0;

    /**
     * 初始化taskId种子
     * @param taskId:
     */
    public static void setTaskIdSeed(int taskId) {
        taskIdSeed = taskId;
    }

    public int generalTaskId() {
        return ++taskIdSeed;
    }

    public HkAgvTask() {

    }

    public HkAgvTask(String startPrePoint, String startPoint, String endPrePoint, String endPoint, int taskType) {
        this.startPrePoint = startPrePoint;
        this.startPoint = startPoint;
        this.endPrePoint = endPrePoint;
        this.endPoint = endPoint;

        Date date = new Date();
        this.createTime = date;

        this.isExec = false;
        this.taskId = generalTaskId();
        this.taskType = taskType;
        this.taskPro = 1;
        this.priority = 0;
        this.taskStatus = HkAgvTaskStatus.INIT.getValue();

        dataStore(this);
    }

    /**
     * 是否执行
     */
    private Boolean isExec;

    /**
     * 任务Id
     */
    private Integer taskId;

    /**
     * 任务进度
     */
    private Integer taskPro;

    /**
     * 任务状态
     */
    private Integer taskStatus;

    /**
     * 优先级
     */
    private Integer priority;

    /**
     * 任务类型
     */
    private Integer taskType;

    /**
     * 托盘码
     */
    private String palletCode;

    /**
     * 取货点
     */
    private String startPoint;

    /**
     * 取货点外面
     */
    private String startPrePoint;

    /**
     * 放货点
     */
    private String endPoint;

    /**
     * 放货点外面
     */
    private String endPrePoint;

    /**
     * wms任务id
     */
    private String wmsTaskId;

    /**
     * 库区
     */
    private String district;

    /**
     * rgv任务终点
     */
    private String rgvTaskEndNode;

    /**
     * agv Id
     */
    private String agvId;

    /**
     * 需要上报wms
     */
    private Boolean needReportWms;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 执行时间
     */
    private Date executeTime;

    /**
     * 完成时间
     */
    private Date completeTime;

    /**
     * 上报时间
     */
    private Date reportTime;

    public Boolean getIsExec() {
        return isExec;
    }

    public void setIsExec(Boolean exec) {
        isExec = exec;
    }

    public Integer getTaskId() {
        return taskId;
    }

    public void setTaskId(Integer taskId) {
        this.taskId = taskId;
    }

    public Integer getTaskPro() {
        return taskPro;
    }

    public void setTaskPro(Integer taskPro) {
        this.taskPro = taskPro;
    }

    public Integer getTaskStatus() {
        return taskStatus;
    }

    public void setTaskStatus(Integer taskStatus) {
        this.taskStatus = taskStatus;
    }

    public Integer getPriority() {
        return priority;
    }

    public void setPriority(Integer priority) {
        this.priority = priority;
    }

    public Integer getTaskType() {
        return taskType;
    }

    public void setTaskType(Integer taskType) {
        this.taskType = taskType;
    }

    public String getPalletCode() {
        return palletCode;
    }

    public void setPalletCode(String palletCode) {
        this.palletCode = palletCode;
    }

    public String getStartPoint() {
        return startPoint;
    }

    public void setStartPoint(String startPoint) {
        this.startPoint = startPoint;
    }

    public String getStartPrePoint() {
        return startPrePoint;
    }

    public void setStartPrePoint(String startPrePoint) {
        this.startPrePoint = startPrePoint;
    }

    public String getEndPoint() {
        return endPoint;
    }

    public void setEndPoint(String endPoint) {
        this.endPoint = endPoint;
    }

    public String getEndPrePoint() {
        return endPrePoint;
    }

    public void setEndPrePoint(String endPrePoint) {
        this.endPrePoint = endPrePoint;
    }

    public String getWmsTaskId() {
        return wmsTaskId;
    }

    public void setWmsTaskId(String wmsTaskId) {
        this.wmsTaskId = wmsTaskId;
    }

    public String getDistrict() {
        return district;
    }

    public void setDistrict(String district) {
        this.district = district;
    }

    public String getRgvTaskEndNode() {
        return rgvTaskEndNode;
    }

    public void setRgvTaskEndNode(String rgvTaskEndNode) {
        this.rgvTaskEndNode = rgvTaskEndNode;
    }

    public String getAgvId() {
        return agvId;
    }

    public void setAgvId(String agvId) {
        this.agvId = agvId;
    }

    public Boolean getNeedReportWms() {
        return needReportWms;
    }

    public void setNeedReportWms(Boolean needReportWms) {
        this.needReportWms = needReportWms;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getExecuteTime() {
        return executeTime;
    }

    public void setExecuteTime(Date executeTime) {
        this.executeTime = executeTime;
    }

    public Date getCompleteTime() {
        return completeTime;
    }

    public void setCompleteTime(Date completeTime) {
        this.completeTime = completeTime;
    }

    public Date getReportTime() {
        return reportTime;
    }

    public void setReportTime(Date reportTime) {
        this.reportTime = reportTime;
    }

    @Override
    public String toString() {
        return "HkAgvTask{" +
                "isExec=" + isExec +
                ", taskId=" + taskId +
                ", taskPro=" + taskPro +
                ", priority=" + priority +
                ", taskType=" + taskType +
                ", palletCode='" + palletCode + '\'' +
                ", startPoint='" + startPoint + '\'' +
                ", startPrePoint='" + startPrePoint + '\'' +
                ", endPoint='" + endPoint + '\'' +
                ", endPrePoint='" + endPrePoint + '\'' +
                ", wmsTaskId='" + wmsTaskId + '\'' +
                ", district='" + district + '\'' +
                ", rgvTaskEndNode='" + rgvTaskEndNode + '\'' +
                ", agvId='" + agvId + '\'' +
                ", needReportWms=" + needReportWms +
                ", createTime=" + createTime +
                ", executeTime=" + executeTime +
                ", completeTime=" + completeTime +
                ", reportTime=" + reportTime +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        HkAgvTask hkAgvTask = (HkAgvTask) o;
        return Objects.equals(isExec, hkAgvTask.isExec) &&
                Objects.equals(taskId, hkAgvTask.taskId) &&
                Objects.equals(taskPro, hkAgvTask.taskPro) &&
                Objects.equals(taskStatus, hkAgvTask.taskStatus) &&
                Objects.equals(priority, hkAgvTask.priority) &&
                Objects.equals(taskType, hkAgvTask.taskType) &&
                Objects.equals(palletCode, hkAgvTask.palletCode) &&
                Objects.equals(startPoint, hkAgvTask.startPoint) &&
                Objects.equals(startPrePoint, hkAgvTask.startPrePoint) &&
                Objects.equals(endPoint, hkAgvTask.endPoint) &&
                Objects.equals(endPrePoint, hkAgvTask.endPrePoint) &&
                Objects.equals(wmsTaskId, hkAgvTask.wmsTaskId) &&
                Objects.equals(district, hkAgvTask.district) &&
                Objects.equals(rgvTaskEndNode, hkAgvTask.rgvTaskEndNode) &&
                Objects.equals(agvId, hkAgvTask.agvId) &&
                Objects.equals(needReportWms, hkAgvTask.needReportWms) &&
                Objects.equals(createTime, hkAgvTask.createTime) &&
                Objects.equals(executeTime, hkAgvTask.executeTime) &&
                Objects.equals(completeTime, hkAgvTask.completeTime) &&
                Objects.equals(reportTime, hkAgvTask.reportTime);
    }

    @Override
    public int hashCode() {
        return Objects.hash(isExec, taskId, taskPro, taskStatus, priority, taskType, palletCode, startPoint, startPrePoint, endPoint, endPrePoint, wmsTaskId, district, rgvTaskEndNode, agvId, needReportWms, createTime, executeTime, completeTime, reportTime);
    }

    private HkAgvTask tempTask = null;
    private void dataStore(HkAgvTask task) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        analyzeData(task);
                    } catch (Exception e) {
                        WcsUtil.exceptionLog(e, this.getClass());
                        WcsUtil.delay(100);
                    }
                }
            }
        }).start();
    }

    private void analyzeData(HkAgvTask task) {
        if (tempTask == null) {
            HkAgvExternFunc.hkAgvTaskDataStore(task);
            tempTask = new HkAgvTask();
            BeanUtils.copyProperties(task, tempTask);
            return;
        }

        if (!tempTask.equals(task)) {
            HkAgvExternFunc.hkAgvTaskDataStore(task);
            BeanUtils.copyProperties(task, tempTask);
        }
    }
}
